//
//  WHSLocalizableConfig.m
//  WHStudy
//
//  Created by GeDaTing on 2021/8/21.
//

#import "WHSLocalizableConfig.h"
#import "NSBundle+WHSLocalizablePath.h"
#import "NSData+WHSTool.h"

@interface WHSLocalizableConfig ()

@property (nonatomic, strong) NSString *language;

@property (nonatomic, strong) NSDictionary *localizableCode;

@end

@implementation WHSLocalizableConfig

+ (instancetype)sharedManager
{
    static WHSLocalizableConfig *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = WHSLocalizableConfig.new;
        [m setupLanguage];
        [m setupLocalizableCode];
    });
    return m;
}

- (void)setupLanguage
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *arr = [defaults objectForKey:@"AppleLanguages"];
    if (arr.count) {
        self.language = [arr objectAtIndex:0];
    } else {
        self.language = @"";
    }
}

- (void)setupLocalizableCode
{
    NSString *path = [[NSBundle whs_getLocalizableBundle] pathForResource:@"WHStudyLocalizable" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    id json = [data whs_jsonSerialization];
    if ([json isKindOfClass:NSDictionary.class]) {
        self.localizableCode = json;
    }
}


+ (NSString *)whs_getLanguage
{
    return [WHSLocalizableConfig sharedManager].language;
}

+ (NSDictionary *)whs_getLocalizableCode
{
    return [WHSLocalizableConfig sharedManager].localizableCode;
}

@end
